package com.zzccyy.jpdemo.fragment

import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.google.gson.Gson
import com.sztle.admodule.apiprocessor.OutRewardAdApi
import com.zzccyy.jpdemo.R
import com.zzccyy.jpdemo.base.BaseLazyFragment
import com.zzccyy.jpdemo.bean.AaaBean1
import com.zzccyy.jpdemo.dialog.BtDateDialog
import com.zzccyy.jpdemo.page.AaaActivity
import com.zzccyy.jpdemo.page.JpCityActivity
import com.zzccyy.jpdemo.page.ResultActivity
import com.zzccyy.jpdemo.page.TT1Activity
import com.zzccyy.jpdemo.page.TT2Activity
import com.zzccyy.jpdemo.page.TT3Activity
import com.zzccyy.jpdemo.page.TT4Activity
import com.zzccyy.jpdemo.utils.HttpUtils
import com.zzccyy.jpdemo.utils.HttpUtils.HttpCallback
import java.io.Serializable
import java.text.SimpleDateFormat
import java.util.Date

class Fragment2 : BaseLazyFragment(), View.OnClickListener {
    private var llT: View? = null
    private var llT1: View? = null
    private var llT2: View? = null
    private var llT3: View? = null
    private var llT4: View? = null
    private var llA1: View? = null
    private var llA2: View? = null
    private var ttSearch: TextView? = null
    private var ttSearch1: TextView? = null
    private var ttt1: TextView? = null
    private var ttt2: TextView? = null
    private var tDay: TextView? = null
    private var tDay1: TextView? = null
    private var tAd1: TextView? = null
    private var tAd2: TextView? = null
    private var v1: View? = null
    private var v2: View? = null
    private var llDay: View? = null
    private var ivfr22: View? = null
    private var r1: View? = null
    private var r2: View? = null
    private var r3: View? = null
    private var r4: View? = null
    private var btDateDialog: BtDateDialog? = null
    private var ettt: EditText? = null
    private var start: String? = "北京"
    private var end: String? = "上海"
    private var isType = false

    override fun setLayoutId(): Int {
        return R.layout.fr2
    }

    override fun init(view: View?) {
        llT = view!!.findViewById(R.id.llT)
        llT1 = view.findViewById(R.id.llT1)
        llT2 = view.findViewById(R.id.llT2)
        llT3 = view.findViewById(R.id.llT3)
        llT4 = view.findViewById(R.id.llT4)
        ttSearch = view.findViewById(R.id.ttSearch)
        ttSearch1 = view.findViewById(R.id.ttSearch1)
        ttt1 = view.findViewById(R.id.ttt1)
        ttt2 = view.findViewById(R.id.ttt2)
        tDay = view.findViewById(R.id.tDay)
        tDay1 = view.findViewById(R.id.tDay1)
        v1 = view.findViewById(R.id.v1)
        v2 = view.findViewById(R.id.v2)
        llDay = view.findViewById(R.id.llDay)
        tAd1 = view.findViewById(R.id.tAd1)
        tAd2 = view.findViewById(R.id.tAd2)
        ivfr22 = view.findViewById(R.id.ivfr22)
        llA1 = view.findViewById(R.id.llA1)
        llA2 = view.findViewById(R.id.llA2)
        r1 = view.findViewById(R.id.r1)
        r2 = view.findViewById(R.id.r2)
        r3 = view.findViewById(R.id.r3)
        r4 = view.findViewById(R.id.r4)
        ettt = view.findViewById(R.id.ettt)
        tAd1!!.setText(start)
        tAd2!!.setText(end)
        val sdf = SimpleDateFormat("yyyy年MM月dd日")
        val formattedDate = sdf.format(Date())
        tDay!!.setText(formattedDate)
    }

    override fun loadData() {
        llT1!!.setOnClickListener(this)
        llT2!!.setOnClickListener(this)
        llDay!!.setOnClickListener(this)
        ttSearch!!.setOnClickListener(this)
        ivfr22!!.setOnClickListener(this)
        llA1!!.setOnClickListener(this)
        llA2!!.setOnClickListener(this)
        r1!!.setOnClickListener(this)
        r2!!.setOnClickListener(this)
        r3!!.setOnClickListener(this)
        r4!!.setOnClickListener(this)
        ttSearch1!!.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.r1 -> { //出票时间
                val intent = Intent(activity, TT1Activity::class.java)
                startActivity(intent)
            }

            R.id.r2 -> { //退票说明
                val intent = Intent(activity, TT2Activity::class.java)
                startActivity(intent)
            }

            R.id.r3 -> { //改签说明
                val intent = Intent(activity, TT3Activity::class.java)
                startActivity(intent)
            }

            R.id.r4 -> { //学生票说明
                val intent = Intent(activity, TT4Activity::class.java)
                startActivity(intent)
            }

            R.id.ttSearch1 -> {
                //查询车次
                if (TextUtils.isEmpty(ettt!!.text.toString())) {
                    Toast.makeText(activity, "请输入车次", Toast.LENGTH_SHORT).show()
                    return
                }
                aaaaa()
            }

            R.id.llDay -> {
                if (btDateDialog == null) {
                    btDateDialog =
                        BtDateDialog(activity!!, object : BtDateDialog.OnViewClickListener {
                            override fun setData(data: String?) {
                                tDay!!.text = data
                                tDay1!!.visibility =
                                    if (isToday(data)) View.VISIBLE else View.INVISIBLE
                            }
                        })
                }
                btDateDialog!!.show()
            }

            R.id.llT1 -> {
                llT!!.setBackgroundResource(R.mipmap.icht11)
                llT3!!.visibility = View.VISIBLE
                llT4!!.visibility = View.GONE
                ttSearch!!.visibility = View.VISIBLE
                ttSearch!!.visibility = View.VISIBLE
                ttSearch1!!.visibility = View.GONE
                ttt1!!.setTextColor(ContextCompat.getColor(activity!!, R.color.color_000000))
                ttt2!!.setTextColor(ContextCompat.getColor(activity!!, R.color.color_ffffff))
                v1!!.visibility = View.VISIBLE
                v2!!.visibility = View.INVISIBLE
            }

            R.id.llT2 -> {
                llT!!.setBackgroundResource(R.mipmap.icht22)
                llT3!!.visibility = View.GONE
                llT4!!.visibility = View.VISIBLE
                ttSearch1!!.visibility = View.VISIBLE
                ttSearch!!.visibility = View.GONE
                ttt2!!.setTextColor(ContextCompat.getColor(activity!!, R.color.color_000000))
                ttt1!!.setTextColor(ContextCompat.getColor(activity!!, R.color.color_ffffff))
                v2!!.visibility = View.VISIBLE
                v1!!.visibility = View.INVISIBLE
            }

            R.id.ttSearch -> {
                OutRewardAdApi.showRewardAd(activity, "query_train") {
                    val intent = Intent(activity, ResultActivity::class.java)
                    intent.putExtra("date", tDay!!.text.toString())
                    intent.putExtra("start", tAd1!!.text.toString())
                    intent.putExtra("end", tAd2!!.text.toString())
                    intent.putExtra("from", 2)
                    startActivity(intent)
                }
            }

            R.id.ivfr22 -> {
                if (!isType) {
                    tAd1!!.text = end
                    tAd2!!.text = start
                } else {
                    tAd1!!.text = start
                    tAd2!!.text = end
                }
                isType = !isType
            }

            R.id.llA1 -> {
                val intent = Intent(activity, JpCityActivity::class.java)
                intent.putExtra("from", 2)
                startActivityForResult(intent, 1111)
            }

            R.id.llA2 -> {
                val intent = Intent(activity, JpCityActivity::class.java)
                intent.putExtra("from", 2)
                startActivityForResult(intent, 2222)
            }
        }
    }

    private fun aaaaa() {
        showLoading()
        val url = "https://api.tgelia.com/api/train/getLine"
        val params: MutableMap<String, String> = HashMap()
        params["no"] = ettt!!.text.toString()
        HttpUtils.getInstance().postJson(url, params, object : HttpCallback {
            override fun onSuccess(result: String) {
                dissmissLoading()
                val homeListBean = Gson().fromJson(result, AaaBean1::class.java)
                if (homeListBean != null && homeListBean.code == 200) {
                    val data = homeListBean.data
                    if (data != null && !data.isEmpty()) {
                        val intent = Intent(activity, AaaActivity::class.java)
                        intent.putExtra("data", data as Serializable?)
                        intent.putExtra("nn", ettt!!.text.toString())
                        startActivity(intent)
                    } else {
                        Toast.makeText(activity, "当前车次暂无数据", Toast.LENGTH_SHORT).show()
                    }
                } else {
                    Toast.makeText(activity, "当前车次暂无数据", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(errorMsg: String) {
                dissmissLoading()
                Toast.makeText(activity, "查询失败", Toast.LENGTH_SHORT).show()
            }
        })
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1111 && resultCode == 1000) {
            start = data!!.getStringExtra("city")
            tAd1!!.text = start
        } else if (requestCode == 2222 && resultCode == 1000) {
            end = data!!.getStringExtra("city")
            tAd2!!.text = end
        }
    }
}